package com.yingyi.system.service.impl;

import com.yingyi.basic.query.BaseQuery;
import com.yingyi.basic.service.impl.BaseServiceImpl;
import com.yingyi.basic.util.PageList;
import com.yingyi.system.domain.Menu;
import com.yingyi.system.domain.Role;
import com.yingyi.system.mapper.RoleMapper;
import com.yingyi.system.service.IRoleService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (t_role)表服务层实现类
 *
 * @author Yingyi
 * @since 2022-08-02 10:39:24
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageList<Role> queryData(BaseQuery query) {
        PageList<Role> rolePageList = super.queryData(query);

        List<Role> roles = rolePageList.getData();
        for (Role role : roles) {
            /**
             * 获取前台menuId的回显数据
             */
            List<List<Long>> menuId =  getMenuId(role);
            role.setMenuId(menuId);
        }

        return rolePageList;

    }
    /**
     * 添加role
     *  关联的表格 t_role_menu t_role_permission t_role
     * @param role
     */
    @Transactional
    @Override
    public void insert(Role role) {
        //保存角色的信息
        roleMapper.insert(role);

        //更新中间表
        initRelativeTable(role);
    }



    /**
     * 更新role
     *  1 更新t_role中的字段
     *  2 更新role对应的菜单 t_role_menu
     *  3 更新role对应的权限 t_role_permission
     *  思路： 先更新t_role 删除t_role_menu、t_role_permission中之前关联的数据，
     *        再将新的选择的菜单、权限数据存入t_role_menu 和t_role_permission
     * @param role
     */
    @Transactional
    @Override
    public void update(Role role) {
        roleMapper.update(role);

        roleMapper.deleteRoleMenu(role.getId());
        roleMapper.deleteRolePermission(role.getId());

        initRelativeTable(role);
    }

    /**
     * 删除role
     *  关联表格 t_employee_role t_role_menu t_role_permission t_role
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        roleMapper.deleteEmployeeRole(id);
        roleMapper.deleteRolePermission(id);
        roleMapper.deleteRoleMenu(id);
        roleMapper.delete(id);
    }

    /**
     * role中间表的添加操作
     * @param role
     */
    private void initRelativeTable(Role role){
        if(!role.getMenuId().isEmpty()){
            //将角色绑定的菜单保存到 中间表t_role_menu中
            roleMapper.insertRoleMenu(role.getId(), transformMenuId(role));
        }

        if(!role.getPermissionId().isEmpty()){
            //将角色绑定的权限保存到 中间表t_role_permission中
            roleMapper.insertRolePermission(role.getId(), role.getPermissionId());
        }
    }


    /**
     *
     * @param role
     * @return
     */
    private List<List<Long>> getMenuId(Role role) {

        Long rid = role.getId();
        /**
         * 通过roleId级联查询menu
         */
        List<Menu> menus = roleMapper.queryRoleMenuByRoleId(rid);

        List<List<Long>> menuId = new ArrayList<>();

        for (Menu menu : menus) {
            List<Long> temp = new ArrayList<>();
            temp.add(menu.getParentId());
            temp.add(menu.getId());
            menuId.add(temp);
        }
        return menuId;

    }



    /**
     * 将前端提交的[[], [], [], [], []]二维数组进行转换为[]一维Long数组
     * @param role
     * @return
     */
    private List<Long> transformMenuId(Role role) {
        List<List<Long>> menuId = role.getMenuId();
        List<Long> ids = menuId.stream().map(list -> list.get(list.size() - 1)).collect(Collectors.toList());

        return ids;
    }



}
